﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Data;

using ZD_Utility;

using ZD_Utility.ZD_Common;

namespace KaoQin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, Regex> regDic = new Dictionary<string, Regex> {
          {"RuzhiLizhi",new Regex("(?<day>\\d{1,2})日(?<type>[入离]职)")},
           {"Leave",new Regex("(?<day>\\d{1,2})日(?<ampm>[上午]?[下午]?)(?<type>.假|旷工)")},
           {"DaKa",new Regex("(?<day>\\d{1,2})日(?<ampm>[早晚]?)未打卡")},
           {"DiaoXiu",new Regex("(?<month>\\d{1,2})月(?<day>\\d{1,2})日加班调休至(?<month1>\\d{1,2})月(?<day1>\\d{1,2})日")}
        };

        string kaoqin = string.Empty;
        KaoQin model;

        private void button1_Click(object sender, EventArgs e)
        {
            model = new KaoQin();
            this.openFileDialog1.ShowDialog();
            kaoqin = this.Txt_kaoqin.Text;

            if (string.IsNullOrEmpty(kaoqin))
            {
                MessageBox.Show("请输入考情情况！");
                return;
            }


            foreach (string item in regDic.Keys)
            {
                switch (item)
                {
                    case "RuzhiLizhi":
                        RuzhiLizhi("RuzhiLizhi");
                        break;
                    case "DaKa":
                        WeiDaKa("DaKa");
                        break;
                    case "DiaoXiu":
                        DiaoXiu("DiaoXiu");
                        break;
                    case "Leave":
                        Leave("Leave");
                        break;
                    default:
                        break;
                }
            }

            this.Lb_bingjia.Text = model.BingJia.ToString();
            this.Lb_diaoxiu.Text = model.DiaoXiu.ToString();
            this.Lb_kuanggong.Text = model.KuangGong.ToString();
            this.Lb_nianjia.Text = model.NianJia.ToString();
            this.Lb_shijia.Text = model.ShiJia.ToString();
            this.Lb_weidaka.Text = model.WeiDaKa.ToString();
            this.Lb_WorkDays.Text = model.Total.ToString();

        }

        public void RuzhiLizhi(string reg)
        {
            var matchs = regDic[reg].Matches(kaoqin);
            int day = 0;
            string type = string.Empty;
            foreach (Match item in matchs)
            {
                type = item.Groups["type"].Value;
                day = item.Groups["day"].Value.ToInt();
                model.Total = CalucateWorkDays(type, day);
            }
        }

        public void Leave(string reg)
        {
            var matchs = regDic[reg].Matches(kaoqin);
            double value = 1;
            string type = string.Empty;
            string ampm = string.Empty;

            foreach (Match item in matchs)
            {
                type = item.Groups["type"].Value;
                ampm = item.Groups["ampm"].Value;

                if (!ampm.IsNullOrEmpty())
                    value = value * 0.5;
                if (type == "病假")
                    value = value * 0.5;

                Calucate(type, value);
                value = 1;
            }
        }

        public void WeiDaKa(string reg)
        {
            var matchs = regDic[reg].Matches(kaoqin);

            foreach (Match item in matchs)
            {
                Calucate("未打卡", 1);
            }
        }

        public void DiaoXiu(string reg)
        {
            var matchs = regDic[reg].Matches(kaoqin);

            foreach (Match item in matchs)
            {
                Calucate("调休", 1);
            }
        }



        public void Calucate(string type, double value)
        {
            switch (type)
            {
                case "旷工":
                    model.KuangGong += value;
                    model.Total -= value;
                    break;
                case "事假":
                    model.ShiJia += value;
                    model.Total -= value;
                    break;
                case "病假":
                    model.BingJia += value;
                    model.Total -= value;
                    break;
                case "年假":
                    model.NianJia += value;
                    break;
                case "调休":
                    model.DiaoXiu += value;
                    break;
                case "未打卡":
                    model.WeiDaKa += value;
                    model.Total -= value;
                    break;
                default:
                    break;
            }
        }

        public double CalucateWorkDays(string type, int Day)
        {
            int currentMonthDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            DateTime now = DateTime.Now;
            double workDays = 0;
            //入职
            if (type == "入职")
            {
                for (int i = Day; i < currentMonthDays; i++)
                {
                    DateTime time = new DateTime(now.Year, now.Month, i);
                    var a = time.DayOfWeek;
                    if (time.DayOfWeek != DayOfWeek.Sunday && time.DayOfWeek != DayOfWeek.Saturday)
                        workDays++;
                }
            }
            else if (type == "离职")
            {
                //离职
                for (int i = 1; i < Day; i++)
                {
                    DateTime time = new DateTime(now.Year, now.Month, i);
                    if (time.DayOfWeek != DayOfWeek.Sunday && time.DayOfWeek != DayOfWeek.Saturday)
                        workDays++;
                }
            }

            return workDays;
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string name = this.openFileDialog1.FileName;
            FileStream fs = new FileStream(name, FileMode.Open);
            DataTable dt = Common.ImportExcelByNPOI(fs);
            foreach (DataRow item in dt.Rows)
            {
                if (item[0] == null || item[0].ToString() == "部  门")
                    continue;
                else if (item[6].ToString().Trim().IsNullOrEmpty())
                {
                    item[2] = model.Total;
                    continue;
                }
                kaoqin = item[6].ToString().Trim();
                foreach (string regItem in regDic.Keys)
                {
                    switch (regItem)
                    {
                        case "RuzhiLizhi":
                            RuzhiLizhi("RuzhiLizhi");
                            break;
                        case "DaKa":
                            WeiDaKa("DaKa");
                            break;
                        case "DiaoXiu":
                            DiaoXiu("DiaoXiu");
                            break;
                        case "Leave":
                            Leave("Leave");
                            break;
                        default:
                            break;
                    }
                }
                item[2] = model.Total;
                item[3] = model.BingJia + model.ShiJia + model.NianJia;
                item[4] = model.KuangGong;
                item[5] = model.WeiDaKa;
                model = new KaoQin();
            }

            Stream stream = Common.ExportExcelByNPOI(dt);
            FileStream file = File.Create("/test.xls");
            int readCounts = 0;//实际读取自己数
            byte[] buffer = new byte[32 * 1024];
            do
            {
                readCounts = stream.Read(buffer, 0, buffer.Length);
                file.Write(buffer, 0, readCounts);
            }
            while (readCounts > 0);

            stream.Dispose();
            file.Dispose();
        }

    }




    public class KaoQin
    {
        public KaoQin()
        {
        }

        private double _total = 21.75;

        public double ShiJia { get; set; }
        public double BingJia { get; set; }
        public double KuangGong { get; set; }
        public double NianJia { get; set; }
        public double DiaoXiu { get; set; }
        public double WeiDaKa { get; set; }
        public double Total { get { return _total; } set { _total = value; } }

    }
}
